﻿using DotNetMVC.OA.IBLL;
using DotNetMVC.OA.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace DotNetMVC.OA.UI.Portal.Controllers
{

    public class HomeController : BaseController
    {
        public IUserInfoService UserInfoService { get; set; }
        public IRoleInfoService RoleInfoService { get; set; }
        public IActionInfoService ActionInfoService { get; set; }
        short delFlagNormal = (short)Model.Enum.DelFlagEnum.Normal;
        // GET: Home
        public ActionResult Index()
        {
            ViewBag.AllMenu = LoadUserMenu();
            return View();
        }

        public List<ActionInfo> LoadUserMenu()
        {
            //获取当前用户
            long userId = this.LoginUser.ID;
            var user = UserInfoService.GetEntities(u => u.ID == userId).FirstOrDefault();

            //获取用户角色
            var allRoleIds = user.R_UserInfo_RoleInfo.Select(r => r.RoleInfoID);
            var allRole = RoleInfoService.GetEntities(r => allRoleIds.Contains(r.ID));

            //获取角色权限Id
            var allRoleActionIds = (from r in allRole
                                    from a in r.R_RoleInfo_ActionInfo
                                    select a.ActionInfoID).ToList().Distinct();
            //获取用户禁用特殊权限Id
            var allDenyUserActionIds = (from r in user.R_UserInfo_ActionInfo
                                        where r.HasPermission == false && r.DelFlag == delFlagNormal
                                        select r.ActionInfoID).ToList();
            //获取用户拥有权限Id
            //角色权限Id 不包含 禁用特殊权限Id
            var allActionIds = allRoleActionIds.Where(a => !allDenyUserActionIds.Contains(a)).ToList();

            //获取用户拥有特殊权限Id
            var allUserActionIds = (from r in user.R_UserInfo_ActionInfo
                                    where r.HasPermission == true && r.DelFlag == delFlagNormal
                                    select r.ActionInfoID).ToList();

            //用户权限Id 包含 拥有特殊权限Id
            allActionIds.AddRange(allUserActionIds);
            allActionIds = allActionIds.Distinct().ToList();

            //获取用户权限信息
            var actionList = ActionInfoService.GetEntities(a => allActionIds.Contains(a.ID)
                                                            && a.IsMenu == true
                                                            && a.DelFlag == delFlagNormal).ToList(); ;
            return actionList;
        }
    }
}